// index.js
// 获取应用实例
const app = getApp()
Page({
  data: {
    username: '',
    password: ''
  },
  onLoad: function() {
  },
  setUsername: function(option) {
    this.setData({
      username: option.detail.value
    })
  },
  setPassword: function(option) {
    this.setData({
      password: option.detail.value
    })
  },
  login: function() {
    wx.request({
      url: app.apiHost + 'user/login',
      data: {
        username: this.data.username,
        password: this.data.password
      },
      method: 'POST',
      success: function(res) {
        var result = res.data
        if(result.status == 200) {
          wx.setStorageSync('user_id', result.data.id)
          wx.navigateTo({
            url: '../lists/lists',
          })
        } else {
          wx.showToast({
            title: result.message,
            icon: 'none'
          })
        }
      }
    })
  },
  auth: function() {
    wx.login({
      success: (res) => {
        console.log(res)
        wx.request({
          url: app.apiHost + 'user/auth',
          data: {
            code: res.code
          },
          method: 'POST',
          success: function(res) {
            var result = res.data
            console.log(result)
            if(result.status == 200) {
              wx.setStorageSync('openid', result.data.openid)
              if(result.data.is_perfect == 1) {
                wx.navigateTo({
                  url: '../lists/lists',
                })
              } else {
                wx.showModal({
                  title: '提示',
                  content: '请完善个人信息',
                  complete: (res) => {
                    if (res.confirm) {
                      wx.navigateTo({
                        url: '../perfect/perfect',
                      })
                    }
                  }
                })
              }
            }
          }
        })
      },
    })
  }
})
